<template>
    <div class="main">
        <div class="bindingidcard">
            <p class="ft14 cl999 mg_l12 mg_tb12">为了您的资金安全，提现需要绑定身份证号码</p>
            <div class="bjcl_fff wh_100" style="margin-bottom: 30px;">
                <Labelinput :placeholder="nameText" :labelcopy="'真实姓名 '" :width="64" :maxlength="16" :cbfn="'nametirgget'" @nametirgget="bindingName" :isrealname="isrealname"></Labelinput>
                <Labelinput :placeholder="'请输入身份证号码'" :inptype="'text'" :maxlength="18" :labelcopy="'身份证号'" :width="64" :cbfn="'idtirgget'" @idtirgget="bindingId"></Labelinput>
            </div>
            <Confimbtn :copy="'保 存'" :disablebtn="disablebtn" :cbfn="cbfn" @confimtrigger="bingdingSubmit"></Confimbtn>
        </div>
    </div>
    <!-- 组件的模板 -->
        
</template>

<script>
    /**
     *  引入工具类库 或 Native API
     *  example : import widget from 'gapi/widget'
     */

    import * as util from 'js/util.js'
    import widget from 'gapi/widget'
    import safety from 'gapi/safety.js'   
    import cache from 'gapi/cache.js'  
    import commonPage from 'clientApi/commonPage.js'
    // import ApiClientUi from 'clientApi/ApiClientUi.js'
    /**
     *  引入组件 以及 组件接口(Action)
     *  example : import Somecomponents from 'components/Somecomponents/Somecomponents'
     */

    import Labelinput from 'components/Labelinput/Labelinput'
    import Confimbtn from 'components/Confimbtn/Confimbtn'

    /**
     *  组件实例对象
     */
    export default {
        components: {
            Labelinput,
            Confimbtn
        },
        data: function(){
            return{
                disablebtn:true,
                cbfn:'confimtrigger',
                name:'',
                id:'',
                valChange: '',
                last_page: '',
                junUrl: '',
                nameText: '请输入真实姓名',        
                isrealname: true,        
            }
        },
        methods: {
            /**
             * 初始化
             * @method init
             * @public
             * @return {Null} void
             */
            init (){
                this.last_page = util.getUrddataString('last_pageUrl');
                switch(this.last_page){
                    case '1':
                        this.junUrl = 'BindingAlipay'
                        break;
                    case '2':
                        this.junUrl = 'BindingWxpay'
                        break;
                    case '3':
                        this.junUrl = 'BindingBankcard'
                        break;
                }  
                let real_name = util.getUrddataString('real_name')
                if(real_name != '' && real_name != undefined && real_name != 'null'){
                    this.name = real_name
                    this.nameText = real_name
                    this.isrealname = true
                }else{
                    this.isrealname = false
                }    
            },
            bingdingSubmit(){


                this.disablebtn = true;
                if(!/^[\u4E00-\u9FA5\·]+$/gi.test(this.name)){
                    widget.toast('请输入正确的姓名');
                    this.disablebtn = false;
                    return;
                }
                let idcheck = this.identityCodeValid(this.id);
                if(!idcheck){
                    widget.toast('身份证号格式错误');
                    this.disablebtn = false;
                    return;
                }
                commonPage.loadPageStatus('loading')                
                util.ajax({
                    url:'/user/real_name_authentication',
                    type:'GET',
                    data:{
                        name: this.name,
                        idnumber: this.id,
                    },
                    timeout:10000,
                    success:(data) => {
                        commonPage.loadPageStatus('loadSuccess')
                        this.disablebtn = false;
                        if(data.errno == 0){
                            let encodeStr = encodeURIComponent(`real_name=${this.name}`)
                            location.href = `dmsaas://${this.junUrl}?urdData=${encodeStr}`;

                        }else if(data.errno == 29001){
                            widget.toast("实名认证失败，姓名与身份证不匹配")
                        }else{
                            widget.toast(data.errmsg)
                        }
                    },
                    error:function(status){
                        commonPage.loadPageStatus('loadSuccess')
                        this.disablebtn = false;
                        widget.toast('服务器异常')
                    },
                    noNetwork: () => {
                        // 无网络的回调
                        this.disablebtn = false;
                        commonPage.loadPageStatus('loadSuccess')
                        widget.toast('网络连接失败，请检查网络')
                    }
                })
            },
            bindingName(val,target){
                let znVal = val.replace(/[^\u4E00-\u9FA5\·]/g,'')
                target.value = znVal;
                this.name = znVal;
            },
            bindingId(val,target){
                let znId;
                if(val.length <= 17){
                    znId = val.replace(/\D/g,'')
                }else{
                    val = val.substr(0,18);
                    if(val.charAt(17)  === 'x' || val.charAt(17)  === 'X'){
                        znId = val;
                    }else{
                        znId = val.replace(/\D/g,'')
                    }
                }
                target.value = znId;
                this.id = znId;
            },
            identityCodeValid(code){
                let city={11:"北京",12:"天津",13:"河北",14:"山西",15:"内蒙古",21:"辽宁",22:"吉林",23:"黑龙江 ",31:"上海",32:"江苏",33:"浙江",34:"安徽",35:"福建",36:"江西",37:"山东",41:"河南",42:"湖北 ",43:"湖南",44:"广东",45:"广西",46:"海南",50:"重庆",51:"四川",52:"贵州",53:"云南",54:"西藏 ",61:"陕西",62:"甘肃",63:"青海",64:"宁夏",65:"新疆",71:"台湾",81:"香港",82:"澳门",91:"国外 "};
                let tip = "";
                let pass= true;
                if(!code || !/^\d{6}(18|19|20)?\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/i.test(code)){
                    tip = "身份证号格式错误";
                    pass = false;
                }
                else if(!city[code.substr(0,2)]){
                    tip = "地址编码错误";
                    pass = false;
                }
                else{
                    //18位身份证需要验证最后一位校验位
                    if(code.length == 18){
                        code = code.split('');
                        if(code[17] === 'x')code[17]='X'
                        //∑(ai×Wi)(mod 11)
                        //加权因子
                        let factor = [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 ];
                        //校验位
                        let parity = [ 1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2 ];
                        let sum = 0;
                        let ai = 0;
                        let wi = 0;
                        for (let i = 0; i < 17; i++)
                        {
                            ai = code[i];
                            wi = factor[i];
                            sum += ai * wi;
                        }
                        let last = parity[sum % 11];
                        if(parity[sum % 11] != code[17]){
                            tip = "校验位错误";
                            pass =false;
                        }
                    }
                }
                return pass;
            }
        },
        computed:{
            valChange:function(){
                return '' + this.name  + this.id
            }
        },
        watch:{
            valChange:function(){
                if(this.name && this.id.length === 18){
                    this.disablebtn = false;
                }else{
                    this.disablebtn = true;
                }
            }
        },
        created(){
            this.init();
            commonPage.updateTitleBar({
                backAction: 'goBack',
                leftAction: 'goBack'
            })
            window.goBack = () => {
                commonPage.goBack(1)
            }
            
        }
        
    }

    
</script>

<!-- CSS 样式 -->
<!-- <style src="static/css/global.css"></style> -->
<style src="../../static/css/base.css"></style>



